import threading
import time

ticket_num = 5
mylock = threading.Lock()


class SaleTicket(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        global ticket_num
        while 1:
            mylock.acquire()
            if ticket_num > 0:
                print(threading.currentThread().getName() + "售出了第" + str(ticket_num) + "号票")
                time.sleep(0.1)
                ticket_num -= 1
            else:
                print("卖完了.")
                break
            mylock.release()


if __name__ == '__main__':
    ticket_window1 = SaleTicket()
    ticket_window2 = SaleTicket()
    ticket_window3 = SaleTicket()
    ticket_window1.start()
    ticket_window2.start()
    ticket_window3.start()
